#include "bsp_pwm.h"
#include "stc15.h"
static void io_init(void);

#define FOSC 11059200L          //系统频率


PWM_Value_t pwm_duty_arr[] = {Duty_0,Duty_20, Duty_40, Duty_60, Duty_80, Duty_100};
void  delay_ms(unsigned char ms);
void delay_s(unsigned char s);


/*
* @name   main
* @brief  主函数
* @param  void	
* @retval int      
*/
int main(void)
{	
	static char i = 0;
	static uint8_t mode = 0;
	//系统初始化
	io_init();
	PWM.PWM_Init();
	//系统主循环
	while(1)
	{
		//按键检测
		//KEY1.KEY_Detect();
		//KEY2.KEY_Detect();
		if(mode == 0)
		{
			PWM.Duty = pwm_duty_arr[i++];
			if(i>4) //这里设置5不知道为什么会出问题
			{	
				mode = 1;
				i = 4;
			}
		}
		else if(mode == 1)
		{
			PWM.Duty = pwm_duty_arr[i--];
			if(i<0)
			{
				mode = 0;
				i = 0;
			}
		}
		PWM.PWM_LED_Adjust_Brightness();
		//delay_s(1);
		delay_ms(170);
	}
}


static void io_init(void)
{
	  P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 = 0x00;
    P7M1 = 0x00;
}



void delay_s(unsigned char s)
{
	unsigned int iter_cnt = (s*1000)/250;
	unsigned int iter;
	for(iter = 0;iter<iter_cnt; iter++)
	{
		delay_ms(250);
	}
}
//========================================================================
// 函数: void  delay_ms(unsigned char ms)
// 描述: 延时函数。
// 参数: ms,要延时的ms数, 这里只支持1~255ms. 自动适应主时钟.
// 返回: none.
// 版本: VER1.0
// 日期: 2013-4-1
// 备注: 
//========================================================================
void  delay_ms(unsigned char ms)
{
     unsigned int i;
     do{
          i = FOSC / 13000;
          while(--i)    ;
     }while(--ms);
}


